package cn.xspace.wnx.activity;

import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.xspace.android.xafcommon.base.BaseActivity;
import com.xspace.android.xafcommon.been.BaseReturnBean;
import com.xspace.android.xafcommon.util.AppUtils;
import com.xspace.android.xafcommon.util.Constants;
import com.xspace.android.xafcommon.util.DateUtils;
import com.xspace.android.xafcommon.util.ErrorCode;
import com.xspace.android.xafcommon.util.EventBusConstant;
import com.xspace.android.xafcommon.util.LogUtil;
import com.xspace.android.xafcommon.util.RequestConstant;
import com.xspace.android.xafcommon.util.ResourceUtils;
import com.xspace.android.xafcommon.util.T;
import com.xspace.android.xafcommon.view.MyGridView;
import com.xspace.android.xafcommon.view.dialog.ButtonClickListener;
import com.xspace.android.xafcommon.view.dialog.DialogHelp;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.xspace.app.view.TitleBar;
import cn.xspace.chatui.bean.MessageEntity;
import cn.xspace.library.bean.Type;
import cn.xspace.library.ui.activity.InfoEditActivity;
import cn.xspace.library.util.BldNavigation;
import cn.xspace.library.util.MsgType;
import cn.xspace.library.util.MyInfoUtil;
import cn.xspace.library.util.OkHttpManager;
import cn.xspace.wnx.R;
import cn.xspace.wnx.adapter.HeadAdapter;
import cn.xspace.wnx.db.ChatRecordDao;
import cn.xspace.wnx.db.MessageRecordDao;
import cn.xspace.wnx.entity.GroupEntity;
import cn.xspace.library.bean.OrgDeptUserBean;
import cn.xspace.wnx.fragment.ChatFragment;
import cn.xspace.wnx.fragment.MyGroupsFragment;
import cn.xspace.wnx.util.Navigator;
import cn.xspace.wnx.view.switchbutton.OnSwitchListener;
import cn.xspace.wnx.view.switchbutton.SwitchButton;
import de.greenrobot.event.EventBus;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/10/12 0012.
 */
public class ChatInfoActivity extends BaseActivity implements View.OnClickListener {

    public static final String TAG = ChatInfoActivity.class.getSimpleName();

    public static final String GROUP_NAME_CHANGE = "groupNameChange";
    public static final String GROUP_DELETE = "groupDelete";
    public static final String CHAT_DELETE = "chatDelete";
    public static final String CHAT_RECORD = "clearChatRecord";
    public static String KEY_MEMBER_OPERATE = "memberOperate";
    public static String GROUP_ID = "groupId";
    public static String GROUP_OWNER_TURN = "groupOwnerTurn";
    public static String GROUP_MEMBER_DELETE = "groupMemberDelete";
    public static String GROUP_OWNER = "groupOwnerId";
    public static String GROUP_MEMBER_NUMBER = "groupMemberNumber";
    public static String GROUP_TYPE = "group_type";//0 普通群组 2 专业群组 3机构群组
    public static String OTHER_BEAN = "otherBean";
    public static String MEMBER_ADD = "memberAdd";
    public static String MEMBER_MIN = "memberMin";
    public static String SENDER_ID = "senderId";
    public static String IS_QUIT = "isquit";

    private boolean isNeedRfreshThis = false;
    // Content View Elements

    private TitleBar mTitle_bar;
    private MyGridView mGridview;
    private LinearLayout mLinear_group_visible_top;
    private RelativeLayout mRelative_group_num;
    private RelativeLayout mRelative_group_name;
    private TextView mTxt_group_num;
    private TextView mTxt_group_name;
    private ImageView mIv_change_group_name;
    private TextView mTxt_belong_org;
    private RelativeLayout mRelativeOrgInfo;

    private LinearLayout mLinear_switch_group;
    private RelativeLayout mRelative_message_disturb;
    private RelativeLayout mRelative_chat_top;
    private RelativeLayout mRelative_save_chat;
    private SwitchButton mMessage_disturb_switch;
    private SwitchButton mChat_top_switch;
    private SwitchButton mSave_chat_switch;

    private RelativeLayout mRelative_look_chat_record;
    private RelativeLayout mRelative_chat_clear;
    private RelativeLayout mRelative_chat_delt;
    private TextView mTxt_chat_delete;

    private LinearLayout mLinear_group_visible_bottom;
    private RelativeLayout mRelative_owner_turn;

    private int chatType = MsgType.CHAT_SINGLE.toNumber();
    private String senderId = ""; //群组是group_id 单聊是sender_id
    private HeadAdapter headAdapter;
    private ArrayList<OrgDeptUserBean> userList;
    private ArrayList<OrgDeptUserBean> userRealList;
    private String user_id;
    private String group_name;
    private String group_owner_name;
    private String group_id;
    private String group_owner_logo;
    private String group_owner_id;
    private int group_type;
    private String group_orgid;
    private String group_orgname;
    private String group_desc;
    private String hx_groupid;
    private int group_members;
    private String group_logo;
    //ture 提醒，false 不提醒
    private int isWarnOrNot = MessageEntity.WarnStatus.WARN.toNumber();
    private boolean isTop = false;
    private boolean isGroup = false;
    private boolean isLocalExsit = false;
    private OrgDeptUserBean newGroiupOwner;
    private int mGroupType = Type.GroupType.TEMP_GROUP;
    //自己机构的ID
    private String myOrgId;
    //对方是否是本机构人员
    private boolean isOurOrgMember;
    private int isQuit = 0; //是否被提出群组
    private String ispower; //是否群主


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(mContext);
    }

    @Override
    protected void getIntentData() {
        super.getIntentData();
        chatType = getIntent().getIntExtra(BldNavigation.CHAT_TYPE, MsgType.CHAT_SINGLE.toNumber());
        mGroupType = getIntent().getIntExtra(BldNavigation.GROUP_TYPE, Type.GroupType.TEMP_GROUP); //默认为临时会话
        senderId = getIntent().getStringExtra(BldNavigation.SENDER_ID);
        isQuit = getIntent().getIntExtra(BldNavigation.IS_QUIT, 0);
        LogUtil.d(TAG, "wubaojie>>>chatType : " + chatType + " senderId : " + senderId + " mGroupType:" + mGroupType);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_chat_info);
//        Bundle bundle = new Bundle();
//        bundle.putString(SENDER_ID, senderId);
//        if (chatType == MessageEntity.MsgType.CHAT_SINGLE.toNumber()) {
//            getSupportFragmentManager().beginTransaction().add(R.id.container, PersonChatInfoFragment.newInstance
//                    (bundle)).commit();
//        } else if (chatType == MessageEntity.MsgType.CHAT_CROUP.toNumber()) {
//            bundle.putInt(GROUP_TYPE, mGroupType);
//            getSupportFragmentManager().beginTransaction().add(R.id.container, GroupChatInfoFragment.newInstance
//                    (bundle)).commit();
//        }
        user_id = MyInfoUtil.getMyUserid(mContext);
        myOrgId = MyInfoUtil.getUserOrgId();
    }

    @Override
    protected void initFindViewById() {
        super.initFindViewById();
        mTitle_bar = (TitleBar) findViewById(R.id.title_bar);
        mTitle_bar.setRightLayoutVisibility(View.GONE);
        mTitle_bar.setTitle("聊天信息");

        mGridview = (MyGridView) findViewById(R.id.gridview);
        mLinear_group_visible_top = (LinearLayout) findViewById(R.id.linear_group_visible_top); //群组上半部分
        mRelative_group_num = (RelativeLayout) findViewById(R.id.relative_group_num);
        mTxt_group_num = (TextView) findViewById(R.id.txt_group_num);
        mRelative_group_name = (RelativeLayout) findViewById(R.id.relative_group_name);
        mTxt_group_name = (TextView) findViewById(R.id.txt_group_name);
        mIv_change_group_name = (ImageView) findViewById(R.id.iv_group_name_arrow);
        mTxt_belong_org = (TextView) findViewById(R.id.txt_belong_org);
        mRelativeOrgInfo = (RelativeLayout) findViewById(R.id.relative_org_info);
        if (mGroupType == Type.GroupType.ORGANIZATION) { //只有机构群组显示机构信息,其他都不显示机构信息
            mRelativeOrgInfo.setVisibility(View.VISIBLE);
        }

        mLinear_switch_group = (LinearLayout) findViewById(R.id.ll_switch_group);
        mRelative_message_disturb = (RelativeLayout) findViewById(R.id.relative_message_disturb);
        mMessage_disturb_switch = (SwitchButton) findViewById(R.id.message_disturb_switch);
        mRelative_chat_top = (RelativeLayout) findViewById(R.id.relative_chat_top);
        mChat_top_switch = (SwitchButton) findViewById(R.id.chat_top_switch);
        mRelative_save_chat = (RelativeLayout) findViewById(R.id.relative_save_group);
        mSave_chat_switch = (SwitchButton) findViewById(R.id.save_group_switch);

//        mLinear_group_visible_bottom = (LinearLayout) findViewById(R.id.linear_group_visible_bottom);
//        mRelative_owner_turn = (RelativeLayout) findViewById(R.id.relative_owner_turn);
//        mRelative_owner_turn.setVisibility(View.GONE);// TODO 群组转让目前先隐藏,后续再开放
//        mRelative_owner_turn.setOnClickListener(this);

        mRelative_look_chat_record = (RelativeLayout) findViewById(R.id.relative_look_chat_record); //查看聊天记录
        mRelative_chat_clear = (RelativeLayout) findViewById(R.id.relative_chat_clear); //情况聊天记录

        mRelative_chat_delt = (RelativeLayout) findViewById(R.id.relative_chat_delt);//删除并退出以及内容显示
        mTxt_chat_delete = (TextView) findViewById(R.id.tv_chat_delete);
        if (isSingle()) {
            mTxt_chat_delete.setText(R.string.txt_chat_delet);
        } else {
            if (isQuit == 1) { //已被踢出群组
                mTxt_chat_delete.setText(R.string.txt_chat_temp);
            }
        }

        mRelative_group_name.setOnClickListener(this); //群组名称条目
        mRelative_group_num.setOnClickListener(this); //群组成员条目
        mRelative_look_chat_record.setOnClickListener(this); //查看聊天记录条目
        mRelative_chat_clear.setOnClickListener(this); //清空聊天记录条目
        mRelative_chat_delt.setOnClickListener(this); //删除条目
        if (isSingle()) {
            mLinear_group_visible_top.setVisibility(View.GONE);
            mRelative_save_chat.setVisibility(View.GONE);
//            mLinear_group_visible_bottom.setVisibility(View.GONE);
        } else {
            mLinear_group_visible_top.setVisibility(View.VISIBLE);
//            mLinear_group_visible_bottom.setVisibility(View.VISIBLE);
            if (isQuit == 1) { //已被踢出群组
                mLinear_switch_group.setVisibility(View.GONE);
                mRelative_chat_clear.setVisibility(View.GONE);
            }
            if (mGroupType == Type.GroupType.PROFESSIONAL || mGroupType == Type.GroupType.ORGANIZATION) {
                //如果是机构群组或者专业群组,不显示保存为群组按钮
                mRelative_save_chat.setVisibility(View.GONE);
            }
        }
        mGridview.setSelector(new ColorDrawable(Color.TRANSPARENT));
    }

    @Override
    protected void initData() {
        if (!AppUtils.isConnected(mContext)) {
            return;
        }
        userList = new ArrayList<>();
        userRealList = new ArrayList<>();
        headAdapter = new HeadAdapter(mContext, userList);
        mGridview.setAdapter(headAdapter);
        showWaitDialog();
//        Map<String, Object> dataMap = new HashMap<>();
//        dataMap.put("user_id", user_id);
//        dataMap.put("org_id", myOrgId);
//        dataMap.put("member_id", senderId);
//        if (chatType == MsgType.CHAT_SINGLE.toNumber()) { //如果是单聊就先获取查询是否是本机构人员
//            registerSubscription(OkHttpManager.getInstance().commonRequest(Constants.CHECK_USER_EXIST_BYORG, dataMap)
//                    .observeOn(Schedulers.io())
//                    .subscribeOn(AndroidSchedulers.mainThread())
//                    .subscribe(new Action1<BaseReturnBean>() {
//                        @Override
//                        public void call(BaseReturnBean baseReturnBean) {
//                            LogUtil.d(TAG, "wubaojie>>>call: baseReturnBean.data : " + baseReturnBean.data);
//                            try {
//                                JSONObject jsonObject = new JSONObject(baseReturnBean.data);
//                                int result = jsonObject.optInt("result");
//                                isOurOrgMember = result == 1;//1:存在 0:不存在
//                            } catch (JSONException e) {
//                                e.printStackTrace();
//                            }
////                            try {
////                                JSONObject jsonObject = new JSONObject(baseReturnBean.data);
////                                sender_orgId = jsonObject.optString("user_orgid");
////                            } catch (JSONException e) {
////                                e.printStackTrace();
////                            }
//                        }
//                    }));
//        }
        registerSubscription(MessageRecordDao.getInstance().getiMessageData().getMessageEntityByKeyId(senderId,
                chatType)
                .observeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(new Action1<MessageEntity>() {
                    @Override
                    public void call(MessageEntity entity) {
                        LogUtil.d(TAG, "wubaojie>>>call: 该会话本地存在?" + (entity != null));
                        if (entity == null) {
                            isLocalExsit = false;
                        } else { //本地存在该会话
                            isWarnOrNot = entity.msg_warn_status;
                            isTop = entity.msg_index != 0;
                            isLocalExsit = true;
                        }
                        if (isSingle()) { //单聊
                            OrgDeptUserBean mineBean = new OrgDeptUserBean();
                            mineBean.user_id = user_id;
                            mineBean.user_name = MyInfoUtil.getName(mContext);
                            mineBean.user_logo = MyInfoUtil.getMyLogo(mContext);
                            userRealList.add(mineBean);
                            if (isLocalExsit) { //本地数据库不存在该条聊天记录
                                OrgDeptUserBean bean = new OrgDeptUserBean();
                                OrgDeptUserBean addBean = new OrgDeptUserBean();
                                addBean.isAddButton = true;
                                //获取单聊
                                bean.user_logo = entity.msg_logo;
                                bean.user_name = entity.msg_sender_name;
                                bean.user_id = senderId;
                                userList.add(bean);
                                userList.add(addBean);
                                userRealList.add(bean);
                            }
                        }
                    }
                })
                .flatMap(new Func1<MessageEntity, Observable<BaseReturnBean>>() {
                    @Override
                    public Observable<BaseReturnBean> call(MessageEntity entity) {
                        LogUtil.d("entity " + (entity == null));
                        if (!isLocalExsit && isSingle()) {
                            //本地不存在，且为单聊,则获取用户信息
                            Map<String, Object> dataMap = new HashMap<>();
                            dataMap.put("user_id", senderId);
                            return OkHttpManager.getInstance().commonRequest(Constants.QUERY_USER_CARD, dataMap);
                        } else if (!isSingle()) {
                            //群聊
                            Map<String, Object> dataMap = new HashMap<>();
                            dataMap.put("user_id", user_id);
                            dataMap.put("group_id", senderId);

                            if (mGroupType == Type.GroupType.PROFESSIONAL) { //如果是专业群组
                                return OkHttpManager.getInstance().commonRequest(Constants.QUERY_PRO_GROUP_INFOR,
                                        dataMap);
                            } else {
                                return OkHttpManager.getInstance().commonRequest(Constants.QUERY_GROUP_INFOR, dataMap);
                            }
                        } else {
                            //单聊并且本地存在数据,使用从数据库拉取的数据即可
                            return Observable.create(new Observable.OnSubscribe<BaseReturnBean>() {
                                @Override
                                public void call(Subscriber<? super BaseReturnBean> subscriber) {
                                    subscriber.onNext(null);
                                }
                            });
                        }
                    }
                })
                .map(new Func1<BaseReturnBean, Object>() {
                    @Override
                    public Object call(BaseReturnBean baseReturnBean) {
                        LogUtil.d("baseReturnBean " + baseReturnBean);
                        if (baseReturnBean == null) { //单聊且为本地
                            return null;
                        } else if (isSingle()) { //单聊,访问网络获取的数据
                            return new Gson().fromJson(baseReturnBean.data, OrgDeptUserBean.class);
                        } else { //群聊
                            List<OrgDeptUserBean> dataList = null;
                            if (ErrorCode.SUCCESS_CODE_WNX.equals(baseReturnBean.code)) {
                                JSONObject dataJson = null;
                                dataList = new ArrayList<OrgDeptUserBean>();
                                try {
                                    dataJson = new JSONObject(baseReturnBean.data);
                                    group_name = dataJson.optString("group_name");
                                    group_orgid = dataJson.optString("group_orgid");
                                    group_orgname = dataJson.optString("group_orgname");
                                    group_desc = dataJson.optString("group_desc");
                                    group_id = dataJson.optString("group_id");
                                    group_type = dataJson.optInt("group_type");
                                    isGroup = group_type != 1;
                                    ispower = dataJson.optString("ispower");
                                    LogUtil.d(TAG, "wubaojie>>>call: ispower:" + ispower);
                                    group_owner_id = dataJson.optString("user_id");
                                    group_owner_name = dataJson.optString("user_name");
                                    hx_groupid = dataJson.optString("hx_groupid");
                                    group_members = dataJson.optInt("group_members");
                                    group_logo = dataJson.optString("group_logo");
                                    mGroupType = dataJson.optInt("group_type");
                                    JSONArray userArray = dataJson.getJSONArray("userList");
                                    for (int i = 0; i < userArray.length(); i++) {
                                        JSONObject json = userArray.getJSONObject(i);
                                        OrgDeptUserBean bean = new OrgDeptUserBean();
                                        bean.user_id = json.optString("user_id");
                                        bean.user_name = json.optString("user_name");
                                        bean.user_logo = json.optString("user_logo");
                                        dataList.add(bean);
                                    }

                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                            return dataList;
                        }
                    }
                })
                .doOnNext(new Action1<Object>() {
                    @Override
                    public void call(Object obj) {
                        if (obj == null) {
                            LogUtil.d("obj " + obj);
                        } else if (isSingle() && obj instanceof OrgDeptUserBean) { //单聊访问网络

                            OrgDeptUserBean orgDeptUserBean = (OrgDeptUserBean) obj;
                            OrgDeptUserBean addBean = new OrgDeptUserBean();
                            if (!isLocalExsit) {
                                MessageEntity messageEntity = new MessageEntity();
                                messageEntity.msg_type = chatType;
                                messageEntity.msg_sender_id = orgDeptUserBean.user_id;
                                messageEntity.msg_logo = orgDeptUserBean.user_logo;
                                messageEntity.msg_sender_name = orgDeptUserBean.user_name;
                                messageEntity.isShow = 0;

                                MessageRecordDao.getInstance().updateMessageList(messageEntity);
                            }

                        } else if (obj instanceof List) { //群聊
                            List<OrgDeptUserBean> dataList = (List<OrgDeptUserBean>) obj;
                            if (!isLocalExsit) {
                                MessageEntity messageEntity = new MessageEntity();
                                messageEntity.msg_type = chatType;
                                messageEntity.msg_group_id = senderId;
                                messageEntity.msg_group_name = group_name;
                                messageEntity.msg_logo = group_logo;
                                messageEntity.isShow = 0;
                                messageEntity.group_member = group_members;
                                MessageRecordDao.getInstance().updateMessageList(messageEntity);
                            }
                        }

                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Object>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        T.showShort(mContext, e.getMessage());
                        hideWaitDialog();
                    }

                    @Override
                    public void onNext(Object obj) {
                        hideWaitDialog();
                        LogUtil.d("obj " + obj);
                        if (obj == null) {
                        } else if (isSingle() && obj instanceof OrgDeptUserBean) { //单聊
                            OrgDeptUserBean orgDeptUserBean = (OrgDeptUserBean) obj;
                            userList.add(orgDeptUserBean);
                            userList.add(getAddMemberBean());
                            userRealList.add(orgDeptUserBean);
                            headAdapter.notifyDataSetChanged();
                        } else if (obj instanceof List) {
                            List<OrgDeptUserBean> dataList = (List<OrgDeptUserBean>) obj;
                            userRealList.addAll(dataList);
                            if (dataList.size() > 14) {
                                userList.addAll(dataList.subList(0, 14));
                            } else {
                                userList.addAll(dataList);
                            }

                            LogUtil.d("userList" + userList.size());
                            if (isCanEdit()) {
                                if (isQuit == 1) {
                                    //被踢出了,不能做增减操作
                                } else {
                                    userList.add(getAddMemberBean());
                                    if (isCanDelete()) {
                                        userList.add(getMinMemberBean());
                                    }
                                }
                            }
                            headAdapter.notifyDataSetChanged();
                            mTxt_group_name.setText(TextUtils.isEmpty(group_name) ? "未设置" : group_name);
                            if ("0".equals(ispower) || !isCanEdit()) { //非群主无法修改群名称并且不能点击添加为群聊
                                mIv_change_group_name.setVisibility(View.GONE);
                                mRelative_save_chat.setVisibility(View.GONE);
                            }
                            mTxt_belong_org.setText(group_orgname);
                            mTxt_group_num.setText(String.format(getResources().getString(R.string.group_chat_all_user),
                                    group_members + ""));
                        }
                        LogUtil.d("isWarnOrNot " + isWarnOrNot);
                        //控制开关状态显示
                        if (isWarnOrNot == MessageEntity.WarnStatus.NOT_WARN.toNumber()) {
                            LogUtil.d("Open ");
                            mMessage_disturb_switch.openButton();
                        } else {
                            LogUtil.d("CLose ");
                            mMessage_disturb_switch.closeButton();
                        }
                        if (isTop) {
                            LogUtil.d("Top ");
                            mChat_top_switch.openButton();
                        } else {
                            LogUtil.d("not Top ");
                            mChat_top_switch.closeButton();
                        }
                        if (isGroup) {
                            LogUtil.d("save group");
                            mSave_chat_switch.openButton();
                        } else {
                            LogUtil.d("change to temp chat");
                            mSave_chat_switch.closeButton();
                        }
                        //控制ui
                        LogUtil.d("group_owner_id>" + group_owner_id);
                        LogUtil.d("user_id>" + user_id);
                        if (!TextUtils.isEmpty(group_owner_id) && group_owner_id.equals(user_id)) {
                            LogUtil.d("111");
//                            mLinear_group_visible_bottom.setVisibility(View.VISIBLE);
                        } else {
                            LogUtil.d("222");
//                            mLinear_group_visible_bottom.setVisibility(View.GONE);
                        }
                        if (!isCanEdit()) {
                            mRelative_chat_delt.setVisibility(View.GONE);
//                            mLinear_group_visible_bottom.setVisibility(View.GONE);
                        }
                    }
                }));
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mTitle_bar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mGridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (userList.get(position).isAddButton) {
                    Bundle addBundler = new Bundle();
                    addBundler.putString(KEY_MEMBER_OPERATE, MEMBER_ADD);
                    if (chatType == MsgType.CHAT_CROUP.toNumber()) { //多人会话增加人员
                        addBundler.putString(GROUP_ID, senderId);
                        addBundler.putString(GROUP_OWNER, group_owner_id);
                        addBundler.putInt(GROUP_TYPE, group_type); //传递群组类型,增删时需要使用
                    } else if (chatType == MsgType.CHAT_SINGLE.toNumber()) { //由单人发起多人会话
//                        if (TextUtils.isEmpty(sender_orgId) || !TextUtils.equals(sender_orgId, myOrgId)) {
                        if (!isOurOrgMember) {
                            //发送人的机构和自己不是一个机构,不允许发起群聊
                            DialogHelp.getMessageDialog(mContext,
                                    ResourceUtils.getString(R.string.not_org_member),
                                    ResourceUtils.getString(R.string.cant_create_group),
                                    ResourceUtils.getString(R.string.i_know), null,
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {

                                        }
                                    }, null).show();
                            return;
                        }
                        addBundler.putString(GROUP_OWNER, user_id);
                        addBundler.putSerializable(SelectMemberActivity.SELECTED_MEMBERS, userRealList);
                    }
//                    addBundler.putSerializable(SelectMemberActivity.SELECTED_MEMBERS, userRealList);
                    LogUtil.d("userList size " + userList.size());
                    Navigator.startNewChatActivity(mContext, addBundler);
                } else if (userList.get(position).isMinButton) { //多人会话删除人员
                    Bundle minBundler = new Bundle();
                    minBundler.putString(KEY_MEMBER_OPERATE, MEMBER_MIN);
                    minBundler.putString(GROUP_ID, senderId);
                    minBundler.putString(GROUP_OWNER, group_owner_id);
                    minBundler.putInt(GROUP_MEMBER_NUMBER, group_members); //传递成员总数,以便再下一个页面显示
                    minBundler.putInt(GROUP_TYPE, group_type); //传递群组类型,增删时需要使用
//                    minBundler.putSerializable(SelectMemberActivity.SELECTED_MEMBERS, userRealList);
                    Navigator.startNewChatActivity(mContext, minBundler);
                } else {
                    Navigator.startPersonCardActivity(mContext, userList.get(position).user_id);
                }

            }
        });

        mMessage_disturb_switch.setOnSwitchListener(new OnSwitchListener() {
            @Override
            public void onSwitchChange() {
                if (mMessage_disturb_switch.getCurrentStatus() == SwitchButton.CLOSE) {
                    LogUtil.d("0");
                    MessageRecordDao.getInstance().getiMessageData().updateMessageWarnStatus(senderId, chatType,
                            MessageEntity.WarnStatus.WARN.toNumber())
                            .subscribeOn(Schedulers.io())
                            .subscribe();

                } else if (mMessage_disturb_switch.getCurrentStatus() == SwitchButton.OPEN) {
                    LogUtil.d("1");
                    MessageRecordDao.getInstance().getiMessageData().updateMessageWarnStatus(senderId, chatType,
                            MessageEntity.WarnStatus.NOT_WARN.toNumber())
                            .subscribeOn(Schedulers.io())
                            .subscribe();
                    if (!isLocalExsit) {
                        MessageRecordDao.getInstance().getiMessageData().updateMessageLastTime(senderId, DateUtils
                                .getNowDateTime(), chatType)
                                .subscribeOn(Schedulers.io())
                                .subscribe();
                    }

                }
            }
        });
        mChat_top_switch.setOnSwitchListener(new OnSwitchListener() {
            @Override
            public void onSwitchChange() {
                if (mChat_top_switch.getCurrentStatus() == SwitchButton.CLOSE) {
                    LogUtil.d("0");
                    MessageRecordDao.getInstance().getiMessageData().cancelTopChatMessage(senderId, chatType)
                            .subscribeOn(Schedulers.io())
                            .subscribe();

                } else if (mChat_top_switch.getCurrentStatus() == SwitchButton.OPEN) {
                    LogUtil.d("1");
                    MessageRecordDao.getInstance().getiMessageData().topChatMessage(senderId, chatType)
                            .subscribeOn(Schedulers.io())
                            .subscribe();

                    if (!isLocalExsit) {
                        MessageRecordDao.getInstance().getiMessageData().updateMessageLastTime(senderId, DateUtils
                                .getNowDateTime(), chatType)
                                .subscribeOn(Schedulers.io())
                                .subscribe();
                    }
                }
            }
        });
        mSave_chat_switch.setOnSwitchListener(new OnSwitchListener() {
            @Override
            public void onSwitchChange() {
                showWaitDialog();
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("user_id", user_id);
                hashMap.put("group_id", group_id);
                int tempGroupType = -1;
                if (mSave_chat_switch.getCurrentStatus() == SwitchButton.CLOSE) {
                    LogUtil.d(TAG, "wubaojie>>>onSwitchChange: 关闭,取消保存群组");
                    tempGroupType = 1;//1为临时会话
                } else if (mSave_chat_switch.getCurrentStatus() == SwitchButton.OPEN) {
                    LogUtil.d(TAG, "wubaojie>>>onSwitchChange: 打开,保存为群组");
                    tempGroupType = 0;//0为群组
                }
                hashMap.put("group_type", tempGroupType);
                final int finalTempGroupType = tempGroupType;
                OkHttpManager.getInstance().commonRequest(Constants.SAVE_AS_USER_GROUP, hashMap)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Subscriber<BaseReturnBean>() {
                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {
                                e.printStackTrace();
                                switch (finalTempGroupType) {
                                    case 0: //群组
                                        T.showShort(mContext, "保存群组失败");
                                        mSave_chat_switch.closeButton();
                                        break;
                                    case 1: //会话
                                        T.showShort(mContext, "取消保存群组失败");
                                        mSave_chat_switch.openButton();
                                        break;
                                }
                            }

                            @Override
                            public void onNext(BaseReturnBean baseReturnBean) {
                                hideWaitDialog();
                                ChatActivity.groupType = finalTempGroupType;
                                switch (finalTempGroupType) {
                                    case 0:
                                        DialogHelp.getMessageDialog(mContext,
                                                ResourceUtils.getString(R.string.save_group_success_context),
                                                ResourceUtils.getString(R.string.save_group_success),
                                                ResourceUtils.getString(R.string.button_ok),
                                                "", new DialogInterface.OnClickListener() {
                                                    @Override
                                                    public void onClick(DialogInterface dialog, int which) {
                                                    }
                                                }, null).show();
                                        break;
                                    case 1:
                                        break;
                                }
                                final MessageEntity messageEntity = new MessageEntity();
                                messageEntity.group_type = finalTempGroupType;
                                messageEntity.msg_group_id = group_id;
                                MessageRecordDao.getInstance().updateGroupType(messageEntity)
                                        .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                                        .subscribe(new Action1<Integer>() {
                                            @Override
                                            public void call(Integer integer) {
                                                if (integer > 0) {
                                                    Map<String, Object> messageMap = new HashMap<>();
                                                    messageMap.put(EventBusConstant.TYPE, EventBusConstant
                                                            .HUANXIN_PUSH);
                                                    messageMap.put(EventBusConstant.HUANXIN_PUSH_BEAN, messageEntity);
                                                    EventBus.getDefault().post(messageMap);
                                                }
                                            }
                                        }); //保存信息到数据库

                            }
                        });
//                OkHttpManager.getInstance().commonRequest(Constants.SAVE_AS_USER_GROUP, hashMap)
//                        .subscribeOn(Schedulers.io())
//                        .observeOn(AndroidSchedulers.mainThread())
//                        .subscribe(new Subscriber<BaseReturnBean>() {
//                            @Override
//                            public void onCompleted() {
//
//                            }
//
//                            @Override
//                            public void onError(Throwable e) {
//                                e.printStackTrace();
//                                T.showShort(mContext, "取消保存群组失败");
//                                mSave_chat_switch.openButton();
//                            }
//
//                            @Override
//                            public void onNext(BaseReturnBean baseReturnBean) {
//                                hideWaitDialog();
//                            }
//                        });
            }
        });

    }

//    Subscriber groupSubscrible =

//    Action1<BaseReturnBean> deleteDb = ;

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.relative_group_name) { //群组名称
//            if (!isCanEdit())
            if (isCanDelete() && isCanEdit()) {
                Bundle mBundler = new Bundle();
                mBundler.putInt(InfoEditActivity.EDIT_TYPE, Type.EditType.GroupNameEdit.getNumber());
                mBundler.putString(InfoEditActivity.EDIT_ID, senderId);
                mBundler.putString(InfoEditActivity.EDIT_VALUE, TextUtils.isEmpty(group_name) ? "" : mTxt_group_name
                        .getText().toString());
                BldNavigation.startInfoEditActivity(mContext, mBundler);
            }

        } else if (v.getId() == R.id.relative_group_num) { //全部成员
            Bundle mBundler = new Bundle();
//            mBundler.putSerializable(SelectMemberActivity.SELECTED_MEMBERS, userList);
            mBundler.putString(GROUP_ID, senderId);
            mBundler.putString(GROUP_OWNER, group_owner_id);
            mBundler.putInt(GROUP_MEMBER_NUMBER, group_members);
            mBundler.putInt(GROUP_TYPE, mGroupType);
            mBundler.putInt(IS_QUIT, isQuit);
            Navigator.startGroupMemberListActivity(mContext, mBundler);
        } else if (v.getId() == R.id.relative_look_chat_record) { //查看聊天记录
            Bundle dataBundle = new Bundle();
            dataBundle.putString(BldNavigation.SENDER_ID, senderId);
            dataBundle.putInt(BldNavigation.CHAT_TYPE, chatType);
            dataBundle.putBoolean(ChatFragment.PAGE_TYPE, true);
//            dataBundle.putString(Navigator.CHAT_NAME, orgBean.dept_name);
//            dataBundle.putString(Navigator.CHAT_SENDER_LOGO, "");
            Navigator.startChatRecordActivity(mContext, dataBundle);
        } else if (v.getId() == R.id.relative_owner_turn) { //转让群组,目前已隐藏
            Bundle mBundler = new Bundle();
//            mBundler.putSerializable(SelectMemberActivity.SELECTED_MEMBERS, userList);
            mBundler.putString(GROUP_ID, senderId);
            mBundler.putInt(BldNavigation.GROUP_TYPE, mGroupType);
            mBundler.putString(GROUP_OWNER, group_owner_id);
            mBundler.putString(EventBusConstant.ACTION_KEY, GROUP_OWNER_TURN);
            mBundler.putInt(GROUP_MEMBER_NUMBER, group_members);
            mBundler.putInt(GROUP_TYPE, mGroupType);
            Navigator.startGroupMemberListActivity(mContext, mBundler);
        } else if (v.getId() == R.id.relative_chat_clear) { //清空聊天记录
            DialogHelp.getConfirmDialog(mContext,
                    getResources().getString(R.string.txt_chat_clear),
                    getResources().getString(R.string.chat_record_clear_confirm),
                    getResources().getString(R.string.think_again_and_again),
                    getResources().getString(R.string.txt_clear),
                    null,
                    new ButtonClickListener() {
                        @Override
                        public void buttonClick(View view) {
                            //清空聊天记录
                            ChatRecordDao.getInstance().getIChatRecordOperation().clearChatRecord(senderId, chatType)
                                    .subscribeOn(Schedulers.io())
                                    .doOnNext(new Action1<Integer>() {
                                        @Override
                                        public void call(Integer integer) {
                                            int result = MessageRecordDao.getInstance().clearMessageRecord(senderId,
                                                    chatType);
                                            LogUtil.d(result + "");
                                        }
                                    })
                                    .subscribe(new Action1<Integer>() {
                                        @Override
                                        public void call(Integer integer) {
                                            Map<String, Object> datamap = new HashMap<String, Object>();
                                            datamap.put(EventBusConstant.TYPE, ChatFragment.TAG);
                                            datamap.put(EventBusConstant.ACTION_KEY, ChatInfoActivity.CHAT_RECORD);
                                            datamap.put(EventBusConstant.ACTION_VALUE, senderId);
                                            EventBus.getDefault().post(datamap);
                                            finish();
                                        }
                                    });
                        }
                    }
            ).show();
        } else if (v.getId() == R.id.relative_chat_delt) { //删除并退出

            if (isSingle()) {
                DialogHelp.getConfirmDialog(mContext,
                        getResources().getString(R.string.txt_chat_delet),
                        getResources().getString(R.string.single_chat_delete_tip),
                        getResources().getString(R.string.think_again_and_again),
                        getResources().getString(R.string.delete),
                        null,
                        new ButtonClickListener() {
                            @Override
                            public void buttonClick(View view) {
                                //清空聊天记录
                                ChatRecordDao.getInstance().getIChatRecordOperation().clearChatRecord(senderId,
                                        chatType)
                                        .subscribeOn(Schedulers.io())
                                        .doOnNext(new Action1<Integer>() {
                                            @Override
                                            public void call(Integer integer) {
                                                MessageRecordDao.getInstance().deleteSingleChat(senderId);
                                            }
                                        })
                                        .subscribe(new Action1<Integer>() {
                                            @Override
                                            public void call(Integer integer) {
                                                Map<String, Object> datamap = new HashMap<String, Object>();
                                                datamap.put(EventBusConstant.TYPE, ChatFragment.TAG);
                                                datamap.put(EventBusConstant.ACTION_KEY, ChatInfoActivity.GROUP_DELETE);
                                                datamap.put(EventBusConstant.ACTION_VALUE, senderId);
                                                EventBus.getDefault().post(datamap);
                                                finish();
                                            }
                                        });
                                //删除群组
//                                Map<String, Object> dataMap = new HashMap<String, Object>();
//                                dataMap.put(RequestConstant.KEY_USER_ID, user_id);
//                                dataMap.put(RequestConstant.KEY_GROUP_ID, senderId);
//                                OkHttpManager.getInstance().commonRequest(Constants.DEL_USER_GROUP, dataMap)
//                                        .subscribeOn(Schedulers.io())
//                                        .doOnNext(deleteDb)
//                                        .observeOn(AndroidSchedulers.mainThread())
//                                        .subscribe(groupSubscrible);

                            }
                        }).show();
            } else {
                DialogHelp.getConfirmDialog(mContext,
                        isQuit == 1 ? getResources().getString(R.string.group_chat_delete_tip2) : getResources()
                                .getString(R.string.group_chat_delete_tip),
                        "",
                        getResources().getString(R.string.think_again_and_again),
                        getResources().getString(R.string.button_ok),
                        null,
                        new ButtonClickListener() {
                            @Override
                            public void buttonClick(View view) {
                                //删除群组
                                if (isQuit == 1) { //如果被踢出了群组,点击事件改为直接删除数据库信息
                                    ChatRecordDao.getInstance().deleteChatRecord(senderId, chatType);
                                    registerSubscription(MessageRecordDao.getInstance().getiMessageData()
                                            .deleteGroupChat(senderId)
                                            .observeOn(Schedulers.io())
                                            .subscribe(new Action1<String>() {
                                                @Override
                                                public void call(String s) {
                                                    Map<String, Object> chatMap = new HashMap<>();
                                                    chatMap.put(EventBusConstant.ACTION_KEY, GROUP_DELETE);
                                                    chatMap.put(EventBusConstant.ACTION_VALUE, senderId);
                                                    chatMap.put(EventBusConstant.TYPE, ChatFragment.TAG);
                                                    EventBus.getDefault().post(chatMap);
                                                    finish();
                                                }
                                            }));
                                    return;
                                }
                                if (group_owner_id.equals(user_id)) {
                                    //群主退出
                                    Map<String, Object> dataMap = new HashMap<String, Object>();
                                    dataMap.put(RequestConstant.KEY_USER_ID, user_id);
                                    dataMap.put(RequestConstant.KEY_GROUP_ID, senderId);
                                    registerSubscription(OkHttpManager.getInstance().commonRequest(Constants
                                            .DEL_USER_GROUP, dataMap)
                                            .subscribeOn(Schedulers.io())
                                            .doOnNext(new Action1<BaseReturnBean>() {
                                                @Override
                                                public void call(BaseReturnBean baseReturnBean) {
                                                    //删除本地数据库
                                                    MessageRecordDao.getInstance().deleteGroupChat(senderId);
                                                    ChatRecordDao.getInstance().deleteChatRecord(senderId, chatType);
                                                }
                                            })
                                            .observeOn(AndroidSchedulers.mainThread())
                                            .subscribe(new Subscriber<BaseReturnBean>() {
                                                @Override
                                                public void onCompleted() {

                                                }

                                                @Override
                                                public void onError(Throwable e) {
                                                    LogUtil.d(e.getMessage());
                                                    T.showShort(mContext, e.getMessage());
                                                }

                                                @Override
                                                public void onNext(BaseReturnBean baseReturnBean) {
//            T.showShort(mContext, R.string.group_chat_delete_success);
                                                    //通知更新UI
                                                    Map<String, Object> chatMap = new HashMap<>();
                                                    chatMap.put(EventBusConstant.ACTION_KEY, GROUP_DELETE);
                                                    chatMap.put(EventBusConstant.ACTION_VALUE, senderId);
                                                    chatMap.put(EventBusConstant.TYPE, ChatFragment.TAG);
                                                    EventBus.getDefault().post(chatMap);
                                                    Map<String, Object> listMap = new HashMap<>();
                                                    listMap.put(EventBusConstant.ACTION_KEY, GROUP_DELETE);
                                                    listMap.put(EventBusConstant.ACTION_VALUE, senderId);
                                                    listMap.put(EventBusConstant.TYPE, MyGroupsFragment.TAG);
                                                    EventBus.getDefault().post(listMap);
                                                    finish();

                                                    //数据库删除相应记录
                                                    MessageRecordDao.getInstance().getiMessageData().deleteGroupChat
                                                            (senderId)
                                                            .observeOn(Schedulers.io())
                                                            .subscribe(new Action1<String>() {
                                                                @Override
                                                                public void call(String s) {
                                                                    LogUtil.d(s);
                                                                }
                                                            });
                                                }
                                            }));

                                } else {
                                    //非群主退出
                                    List<OrgDeptUserBean> deleteList = new ArrayList<OrgDeptUserBean>();
                                    OrgDeptUserBean bean = new OrgDeptUserBean();
                                    bean.user_id = user_id;
                                    deleteList.add(bean);
                                    Map<String, Object> dataMap = new HashMap<String, Object>();
                                    dataMap.put(RequestConstant.KEY_USER_ID, user_id);
                                    dataMap.put(RequestConstant.KEY_GROUP_ID, senderId);
                                    dataMap.put(RequestConstant.KEY_USER_LIST, deleteList);
                                    registerSubscription(OkHttpManager.getInstance().commonRequest(Constants
                                            .EXIT_GROUP, dataMap)
                                            .subscribeOn(Schedulers.io())
                                            .doOnNext(new Action1<BaseReturnBean>() {
                                                @Override
                                                public void call(BaseReturnBean baseReturnBean) {
                                                    //删除本地数据库
                                                    MessageRecordDao.getInstance().deleteGroupChat(senderId);
                                                    ChatRecordDao.getInstance().deleteChatRecord(senderId, chatType);
                                                }
                                            })
                                            .observeOn(AndroidSchedulers.mainThread())
                                            .subscribe(new Subscriber<BaseReturnBean>() {
                                                @Override
                                                public void onCompleted() {

                                                }

                                                @Override
                                                public void onError(Throwable e) {
                                                    LogUtil.d(e.getMessage());
                                                    T.showShort(mContext, e.getMessage());
                                                }

                                                @Override
                                                public void onNext(BaseReturnBean baseReturnBean) {
//            T.showShort(mContext, R.string.group_chat_delete_success);
                                                    //通知更新UI
                                                    Map<String, Object> chatMap = new HashMap<>();
                                                    chatMap.put(EventBusConstant.ACTION_KEY, GROUP_DELETE);
                                                    chatMap.put(EventBusConstant.ACTION_VALUE, senderId);
                                                    chatMap.put(EventBusConstant.TYPE, ChatFragment.TAG);
                                                    EventBus.getDefault().post(chatMap);
                                                    Map<String, Object> listMap = new HashMap<>();
                                                    listMap.put(EventBusConstant.ACTION_KEY, GROUP_DELETE);
                                                    listMap.put(EventBusConstant.ACTION_VALUE, senderId);
//                                                    listMap.put(EventBusConstant., senderId);
                                                    listMap.put(EventBusConstant.TYPE, MyGroupsFragment.TAG);
                                                    EventBus.getDefault().post(listMap);
                                                    finish();
                                                    //数据库删除相应记录
                                                    MessageRecordDao.getInstance().getiMessageData().deleteGroupChat
                                                            (senderId)
                                                            .observeOn(Schedulers.io())
                                                            .subscribe(new Action1<String>() {
                                                                @Override
                                                                public void call(String s) {
                                                                    LogUtil.d(s);
                                                                }
                                                            });
                                                }
                                            }));

                                }


                            }
                        })
                        .setContentVisible(View.GONE).show();
            }

        }
    }


    /**
     * 判断是否可以编辑
     *
     * @return
     */
    private boolean isCanEdit() {
        if (isSingle()) {
            return true;
        } else if (!isSingle() && (mGroupType == Type.GroupType.NORMAL || mGroupType == Type.GroupType.TEMP_GROUP)) {
            return true;
        } else {
            return false;
        }
    }

    private boolean isCanDelete() {
        return !isSingle() && group_owner_id.equals(user_id);
    }


    public OrgDeptUserBean getAddMemberBean() {
        OrgDeptUserBean addBean = new OrgDeptUserBean();
        addBean.isAddButton = true;
        return addBean;
    }

    public OrgDeptUserBean getMinMemberBean() {
        OrgDeptUserBean addBean = new OrgDeptUserBean();
        addBean.isMinButton = true;
        return addBean;
    }

    /**
     * 显示前6个
     *
     * @param dataList
     * @return
     */
    private List<OrgDeptUserBean> getShowHeadList(List<OrgDeptUserBean> dataList) {
        return dataList.subList(0, 5);

    }

    private OrgDeptUserBean buildGroupOwner() {
        OrgDeptUserBean owner = new OrgDeptUserBean();
        owner.user_id = group_owner_id;
        owner.user_name = group_owner_name;
        owner.user_logo = group_owner_logo;
        return owner;
    }

    /**
     * 判断是否为单聊
     *
     * @return
     */
    private boolean isSingle() {
        if (MsgType.CHAT_SINGLE.toNumber() == chatType) {
            return true;
        }
        return false;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return false;
    }


    private void refreshPage() {
        finish();
        startActivity(getIntent());
    }

    /**
     * 此代码判断是否由后台唤醒进入前台
     */
    @Override
    protected void onResume() {
        super.onResume();
        if (isNeedRfreshThis) {
            isNeedRfreshThis = false;
            refreshPage();
        }
    }

    /**
     * 此代码判断是否由前台进入后台
     */
    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public void onEventMainThread(Map<String, Object> inParam) {
        super.onEventMainThread(inParam);
        if (TAG.equals(inParam.get(EventBusConstant.TYPE))) {
            if (GROUP_NAME_CHANGE.equals(inParam.get(EventBusConstant.ACTION_KEY))) {
                mTxt_group_name.setText((String) inParam.get(EventBusConstant.ACTION_VALUE));
                //修改数据库
            } else if (GROUP_OWNER_TURN.equals(inParam.get(EventBusConstant.ACTION_KEY))) {

                newGroiupOwner = (OrgDeptUserBean) inParam.get(EventBusConstant.ACTION_VALUE);
                LogUtil.d(newGroiupOwner.toString());
                LogUtil.d(user_id);
                if (newGroiupOwner == null || TextUtils.isEmpty(newGroiupOwner.user_id) || newGroiupOwner.user_id
                        .equals(user_id)) {
                    return;
                }
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("user_id", user_id);
                dataMap.put("receiver_id", newGroiupOwner.user_id);
                dataMap.put("group_id", senderId);
                registerSubscription(OkHttpManager.getInstance().commonRequest(Constants.TRANSFER_GROUP, dataMap)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Subscriber<BaseReturnBean>() {
                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {
                                T.showShort(mContext, e.getMessage());
                            }

                            @Override
                            public void onNext(BaseReturnBean o) {
                                T.showShort(mContext, o.desc);
//            group_owner_id = newGroiupOwner.user_id;
//            group_owner_name = newGroiupOwner.user_name;
//            mLinear_group_visible_bottom.setVisibility(View.GONE);
                                refreshPage();
                            }
                        }));

            } else if (GROUP_MEMBER_DELETE.equals(inParam.get(EventBusConstant.ACTION_KEY))) {
                //直接刷新页面
                isNeedRfreshThis = true;

            }
        } else if (EventBusConstant.DELETE_FRIEND_PUSH.equals(inParam.get(EventBusConstant.TYPE))) {
            if (EventBusConstant.DELETE_FRIEND_FINISH.equals(inParam.get(EventBusConstant.ACTION_KEY))) {
                //好友关系删除
                if (senderId.equals(inParam.get(EventBusConstant.ACTION_VALUE))) {
                    LogUtil.d("exit ");
                    //关闭聊天信息页面
                    finish();
                }
            }
        } else if (EventBusConstant.EDIT_INFO.equals(inParam.get(EventBusConstant.TYPE))) {
            Type.EditType type = (Type.EditType) inParam.get(EventBusConstant.ACTION_KEY);
            if (type == Type.EditType.GroupNameEdit) {
                String newValue = (String) inParam.get(EventBusConstant.ACTION_VALUE);
                Map<String, Object> infoMap = new HashMap<String, Object>();
                Map<String, Object> chatMap = new HashMap<String, Object>();
                Map<String, Object> listMap = new HashMap<String, Object>();
                infoMap.put(EventBusConstant.TYPE, ChatInfoActivity.TAG);
                chatMap.put(EventBusConstant.TYPE, ChatFragment.TAG);
                listMap.put(EventBusConstant.TYPE, MyGroupsFragment.TAG);
                infoMap.put(EventBusConstant.ACTION_KEY, ChatInfoActivity.GROUP_NAME_CHANGE);
                chatMap.put(EventBusConstant.ACTION_KEY, ChatInfoActivity.GROUP_NAME_CHANGE);
                listMap.put(EventBusConstant.ACTION_KEY, ChatInfoActivity.GROUP_NAME_CHANGE);
                infoMap.put(EventBusConstant.ACTION_VALUE, newValue);
                chatMap.put(EventBusConstant.ACTION_VALUE, newValue);
                GroupEntity entity = new GroupEntity();
                entity.group_id = senderId;
                entity.group_name = newValue;
                listMap.put(EventBusConstant.ACTION_VALUE, entity);
                EventBus.getDefault().post(infoMap);
                EventBus.getDefault().post(chatMap);
                EventBus.getDefault().post(listMap);
                //保存数据库
                MessageRecordDao.getInstance().getiMessageData().updateGroupName(senderId, newValue)
                        .subscribeOn(Schedulers.io())
                        .subscribe();
                Map<String, Object> requestMap = new HashMap<>();

                requestMap.put("user_id", user_id);
                requestMap.put("group_id", group_id);
                requestMap.put("group_name", newValue);
                registerSubscription(OkHttpManager.getInstance().commonRequest(type.getUrl(), requestMap)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Action1<BaseReturnBean>() {
                            @Override
                            public void call(BaseReturnBean baseReturnBean) {
                                hideWaitDialog();
                                LogUtil.d(baseReturnBean.toString());
                            }
                        }, new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                hideWaitDialog();
                                throwable.printStackTrace();
                            }
                        }));
            }
        }
    }
}
